//2308342

#include <main.h>
#define LCD_ENABLE_PIN PIN_C0
#define LCD_RS_PIN PIN_C2
#define LCD_RW_PIN PIN_C1
#define LCD_DATA4 PIN_C4
#define LCD_DATA5 PIN_C5
#define LCD_DATA6 PIN_C6
#define LCD_DATA7 PIN_C7


#include <lcd.c>
// Global variables  
unsigned int16 pot_1 = 0, pot_2 = 0, pot_3 = 0;  
int  button1_prev_state = 0;
int  button2_prev_state = 0;
int  button_counter     = 1;

// function prototypes
void read_pots(void);  // reads pots values from ADC channels
void display_pots(void); // pot values on the display
void button_press(void);  // checks for button press
void spin_motor_counter_clockwise(void); // Spins Motor CCW
void spin_motor_clockwise(void); // spins motor CW
void stop_motor(void); // stops motor
void display_LED(void); // pot values in binary on LEDs

// Void main
void main()
{
   // Configure ADC ports for AN0, AN1, AN2
   setup_adc_ports(sAN0 | sAN1 | sAN2, VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL | ADC_TAD_MUL_0);

   // Initialise the display
   lcd_init();

   // Main loop
   while(TRUE)
   {
      read_pots(); // Question 1
      display_pots(); // Q1
      display_LED(); //Q2
      button_press(); // Q3
   }
}

// functions

// QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQuestion 1		QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQuestion 1		QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQuestion 1
void read_pots(void)
{
   // Pot 1 on AN0 is A
   set_adc_channel(0);
   delay_us(50); // lets value settle if it was changed
   pot_1 = read_adc();

   // Pot 2 on AN1 is B 
   set_adc_channel(1);
   delay_us(50);
   pot_2 = read_adc();

   // Pot 3 on AN2 is C
   set_adc_channel(2);
   delay_us(50);
   pot_3 = read_adc();
}

void display_pots(void)
{
  
   lcd_gotoxy(7, 1);// roughly center line 1
   printf(lcd_putc,"Pot1: %lu", pot_1); // prints value of Pot between 0 and 255

   lcd_gotoxy(7, 2);// roughly center line  2
   printf(lcd_putc,"Pot2: %lu", pot_2); // prints value of Pot between 0 and 255

  
   lcd_gotoxy(27, 1);// roughly center line  3
   printf(lcd_putc,"Pot3: %lu", pot_3); // prints value of Pot between 0 and 255
}


// QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQuestion 2		// QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQuestion 2 		// QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQuestion 2
void display_LED(void)
{
   // button 1 is on pin A4
   if(input(PIN_A4) && !button1_prev_state) // checks if button 1 has been pressed
   {
      // Debounce delay 100ms
      delay_ms(100);
      if(input(PIN_A4))
      {
         // Shows the 8 bits of pot 1s value on the LEDs
         output_b((pot_1 & 0xFF));
         delay_ms(3000);
         output_b(0x00);

         // Shows the 8 bits of pot 2s value on the LEDs
         output_b((pot_2 & 0xFF));
         delay_ms(3000);
         output_b(0x00);

         // Shows the 8 bits of pot 3s value on the LEDs
         output_b((pot_3 & 0xFF));
         delay_ms(3000);
         output_b(0x00);
      }
   }
   button1_prev_state = input(PIN_A4); // updates the state of button 
}

// QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQuestion 3		QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQuestion 3		QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQuestion 3
void button_press(void)
{
   // button 2 is on pin A5
   if(input(PIN_A5) && !button2_prev_state)
   {
     //Debounce delay 100ms     
      delay_ms(100);
      if(input(PIN_A5))  // still pressed
      {
         switch(button_counter) // switch statement counts upto 4
         {
            case 1: // 1 press
               spin_motor_clockwise();               //spins CW
               lcd_gotoxy(23,2);  // roughly center line 4
               printf(lcd_putc, "Buttton2, press 1");
               button_counter++;
               break;

            case 2: // 2 presses
               stop_motor(); 			// Stops motor	
               lcd_gotoxy(23,2);
               printf(lcd_putc, "Buttton2, press 2"); 
               button_counter++;
               break;

            case 3: // 3 presses
               spin_motor_counter_clockwise();  //        spins CCW
               lcd_gotoxy(23,2);
               printf(lcd_putc, "Buttton2, press 3");
               button_counter++;
               break;

            case 4: // 4 presses
               stop_motor(); 				// Stops again
               lcd_gotoxy(23,2);
               printf(lcd_putc, "Button 2, press 4");
               button_counter = 1;
               break;
		  // back to press 1
            default:
               break;
         }
      }
   }
   button2_prev_state = input(PIN_A5); // checks for another press if pressed add 1 to presses goes to next switch case
}


// functions for motor
void spin_motor_counter_clockwise(void) // Allows motor to spin CCW as pin a7 is high and a6 low so wants to spin left to right or CCW
{
   output_low(PIN_A6);
   output_high(PIN_A7);
}

void spin_motor_clockwise(void)     // allows motor to spin CW as a6= high and a7 = low so wants to go right to left or CW
{
   output_low(PIN_A7);
   output_high(PIN_A6);
}

void stop_motor(void)   // Pins Low so no spinning
{
   output_low(PIN_A6);
   output_low(PIN_A7);
}

